<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/27/0027
 * Time: 14:15
 */

namespace app\sports_admin\controller;

use app\exception\AdminException;
use think\Cache;
use think\Controller;
use think\Loader;
use think\Session;
use operation;
use app\sports_admin\model\System as SystemModel;
use Zhuzhichao\IpLocationZh\Ip;

Loader::import('operation.operation', EXTEND_PATH);
class Base extends Controller
{
    private $operation;
    //判断是否登录
    public function _initialize()
    {
        if(!Session::has('sport_admin')){
            $this->redirect('/sport_admin/login');
        }
        $this->operation = new operation\Operation();
        $this->isOpen();
    }
    //判断是否为最高权限
    public function isAdmin()
    {
        if(Session::get('sport_admin.scope') != 128){
            throw new AdminException([
                'msg'=>'权限不足',
                'errorCode'=>403
            ]);
            die();
        }
    }
    //判断是否可以管理用户
    public function isUsers()
    {
        $scope = Session::get('sport_admin.scope');
        if($scope != 8 && $scope != 128){
            throw new AdminException([
                'msg'=>'权限不足',
                'errorCode'=>403
            ]);
            die();
        }
    }
    //判断是否可以管理资源
    public function isResource()
    {
        $scope = Session::get('sport_admin.scope');
        if($scope != 16 && $scope != 128){
            throw new AdminException([
                'msg'=>'权限不足',
                'errorCode'=>403
            ]);
            die();
        }
    }
    // 业务
    public function isBusiness()
    {
        $scope = Session::get('sport_admin.scope');
        if($scope != 32 && $scope != 128){
            throw new AdminException([
                'msg'=>'权限不足',
                'errorCode'=>403
            ]);
            die();
        }
    }
    //财务
    public function isFinance()
    {
        $scope = Session::get('sport_admin.scope');
        if($scope != 64 && $scope != 128){
            throw new AdminException([
                'msg'=>'权限不足',
                'errorCode'=>403
            ]);
            die();
        }
    }
    //数据管理
    public function isData()
    {
        $scope = Session::get('sport_admin.scope');
        if($scope != 72 && $scope != 128){
            throw new AdminException([
                'msg'=>'权限不足',
                'errorCode'=>403
            ]);
            die();
        }
    }
    //检查系统是否开启
    public function isOpen()
    {
        if(Session::get('sport_admin.scope') != 128){
            if(SystemModel::systemStatus() != 1){
                echo "<script>alert('系统已关闭')</script>";
                echo "<script>top.location.href='/login/loginOut'</script>";
                die();
            }
        }
    }
    //查询操作记录
    public function operationSelect()
    {
        $this->operation->select();
    }
    //添加操作记录
    public function operationInsert()
    {
        $this->operation->insert();
    }
    //更新操作记录
    public function operationUpdate()
    {
        $this->operation->update();
    }
    //删除操作记录
    public function operationDelete()
    {
        $this->operation->delete();
    }
}